package site.user.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.ApplyEquity;
import modules.recruit.entity.ShareholderB;
import modules.recruit.service.ApplyEquityService;
import modules.recruit.service.ShareholderBService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.service.EntityService;
import com.guinea.core.utils.StringUtils;
import com.guinea.core.web.StrutsAjaxAction;

public class ShareholderBActionSite extends StrutsAjaxAction {
	private ShareholderBService shareholderBService;

	private ApplyEquityService applyEquityService;

	private ApplyEquity applyEquity;

	private Long applyEquityId;

	public EntityService getEntityService() {
		return shareholderBService;
	}

	public String entity2json(Object object) {
		ShareholderB shareholderB = (ShareholderB) object;
		String result = "";
		result += "{";
		result += "\"id\" : \""
				+ (shareholderB.getId() == null ? "" : shareholderB.getId()
						.longValue()
						+ "") + "\"";
		result += ", \"equityId\" : \""
				+ (shareholderB.getEquity() == null ? "" : shareholderB
						.getEquity().getId().longValue()
						+ "") + "\"";
		result += ", \"shareholderName\" : \""
				+ StringUtils.defaultString(shareholderB.getShareholderName())
				+ "\"";
		result += ", \"stake\" : \""
				+ StringUtils.defaultString(shareholderB.getStake()) + "\"";
		result += ", \"percent\" : \""
				+ StringUtils.defaultString(shareholderB.getPercent()) + "\"";
		result += "}";
		return result;
	}
	
	protected String saveWrapper(String text){
		return "<script>window.parent.successShareholderB('"+text+"')</script>";
	}

	public ShareholderBService getShareholderBService() {
		return shareholderBService;
	}

	public void setShareholderBService(ShareholderBService shareholderBService) {
		this.shareholderBService = shareholderBService;
	}

	public ApplyEquityService getApplyEquityService() {
		return applyEquityService;
	}

	public void setApplyEquityService(ApplyEquityService applyEquityService) {
		this.applyEquityService = applyEquityService;
	}
	
	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form,request);
		applyEquityId = (Long) getEntityId(request, "applyEquityId", Long.class);
		if (applyEquityId != null) {
			applyEquity = getApplyEquityService().getApplyEquity(applyEquityId);
		}
	}
	
	protected void initProperties() {
		super.initProperties();
		applyEquityId = null;
		applyEquity = null;
	}

	protected boolean validateApplyEquity(HttpServletRequest request) {
		if (applyEquity == null) {
			return valid = false;
		}
		return true;
	}
	
	public ActionForward ajaxListValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		initFirst(form,request);
		if (!validateApplyEquity(request))
			return handleApplyEquityMissing(mapping, request, response);
		return null;
	}
	
	/**
	 * 取得SkoruShareholder对象列表
	 */
	public ActionForward ajaxList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List list = getShareholderBService().getByApplyEquity(applyEquityId);
		String result = collection2json(list);
		renderText(response, "{\"data\":"+result+"}");
		return null;
	}
	
	public ActionForward ajaxSaveValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		initFirst(form,request);
		if (!validateApplyEquity(request))
			return handleApplyEquityMissing(mapping, request, response);
		if (!validateForm(mapping, form, request))
			return handleFormErrors(mapping, form, request, response);
		return null;
	}
	
	protected void doInitEntity(HttpServletRequest request,ActionForm form) {
		super.doInitEntity(request, form);
		if(isCreateOperation){
			ShareholderB shareholderB = (ShareholderB) entity;
			shareholderB.setEquity(applyEquity);
			entity = shareholderB;
		}
	}

	protected ActionForward handleApplyEquityMissing(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response) {
		renderText(response, "{\"error\":\"找不到股权结构记录!\"}");
		return null;
	}

}
